﻿using AdminSystem.Application.Addresss.Dto;
using AdminSystem.Domain;
using AdminSystem.Repositories;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using AdminSystem.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

namespace AdminSystem.Application.Addresss
{
    public class AddressService : IAddressService
    {
        private readonly IRepository<Address> addressRepository;
        private readonly IRepository<Admin> adminRepository;
        private readonly AdminSystemDbContext db;
        private readonly IMapper mapper;

        public AddressService(IRepository<Address> addressRepository, IRepository<Admin> adminRepository, AdminSystemDbContext db, IMapper mapper)
        {
            this.addressRepository = addressRepository;
            this.adminRepository = adminRepository;
            this.db = db;
            this.mapper = mapper;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="createUpdateAddressDto"></param>
        /// <returns></returns>
        public async Task<ResultDto<AddressDto>> CreateAsync(CreateUpdateAddressDto createUpdateAddressDto)
        {
            var address = mapper.Map<Address>(createUpdateAddressDto);
            await addressRepository.InsertAsync(address);
            return new ResultDto<AddressDto>
            {
                StatusCode = StatusCode.Success,
                Data = mapper.Map<AddressDto>(address)
            };
        }

        /// <summary>
        /// 查询所有
        /// </summary>
        /// <returns></returns>
        public async Task<ResultDto<List<AddressDto>>> QueryAllAsync()
        {
            var address = await addressRepository.Queryable().ToListAsync();
            return new ResultDto<List<AddressDto>>
            {
                StatusCode = StatusCode.Success,
                Data = mapper.Map<List<AddressDto>>(address)
            };
        }

        

        public async Task<ResultDto<PageResultDto<AddressDto>>> PageAsync(SearchAddressDto searchAddressDto, PageDto pageDto)
        {
            var list = adminRepository.Queryable().Join(addressRepository.Queryable(), a => a.AdminId, b => b.AdminId, (a, b) => new
             AddressDto
            {
                AddressId = b.AddressId,
                Area = b.Area,
                City = b.City,
                Detail = b.Detail,
                Email = b.Email,
                Mobile = b.Mobile,
                Province = b.Province,
                UserName = a.UserName,
                Password = a.Password,
                Gender = a.Gender
            });

            if(!string.IsNullOrWhiteSpace(searchAddressDto.UserName))
            {
                list = list.Where(m => m.UserName.Contains(searchAddressDto.UserName));
            }

            if (!string.IsNullOrWhiteSpace(searchAddressDto.Province))
            {
                list = list.Where(m => m.Province.Contains(searchAddressDto.Province));
            }

            if (!string.IsNullOrWhiteSpace(searchAddressDto.Email))
            {
                list = list.Where(m => m.Email.Contains(searchAddressDto.Email));
            }


            return new ResultDto<PageResultDto<AddressDto>> {
                StatusCode = StatusCode.Success,
                Data = new PageResultDto<AddressDto>
                {
                    TotalCount = list.Count(),
                    Data = list.Skip((pageDto.PageIndex - 1) * pageDto.PageSize).Take(pageDto.PageSize).ToList()
                }
            };
        }
    }
}
